<?php
/**
 * Created by PhpStorm.
 * User: gaojie
 * Date: 16/7/26
 * Time: 上午10:13
 */

namespace app\Http\Controllers;

use App\Http\Requests\CreateDataRequest;
use App\Model\AmbientData;
use App\Model\ConfluxBoxData;
use App\Model\InverterDataThree;
use App\Model\InverterDataTwo;
use App\Model\PowerData;
use Illuminate\Http\Request;
use App\Model\InverterData;
use App\Model\Site;
use Mockery\Exception;
use Redis;


class DataController extends Controller{

    public  function  postCreateInverterData(Request $request)
    {
        $param = $request->all();
        $redis = $this->_Redis();
        $site_id = $request->input('site_id');
        $site = Site::find($site_id);
        $param['user_id'] = $site->user_id;
        $param['created_at'] = date('Y-m-d H:i:s');
//        try {

            switch ($site_id) {
                case $site_id < 18:
                    $param = $this->getRedisOffsetData($site_id, $param['device_id'], $param);
                    $redis->set('test:result',json_encode($param));
                    $obj = InverterData::create($param);
                    $json = json_encode($param);
                    $redis->set('xy:industry:data:'.$site_id.':'.$param['device_id'],$json);
                    if(!$obj)
                    {
                        return $this->json_sys_error();
                    }
                    break;
                case $site_id >= 18 && $site_id < 43:
//                $param = $this->getRedisOffsetData($site_id,$param['device_id'],$param);
                    $obj = InverterDataTwo::create($param);
                    $json = json_encode($param);
                    $redis->set('xy:industry:data:'.$site_id.':'.$param['device_id'],$json);
                    if(!$obj)
                    {
                        return $this->json_sys_error();
                    }
                    break;
                default:
//                $param = $this->getRedisOffsetData($site_id,$param['device_id'],$param);
                    $obj = InverterDataThree::create($param);
                    $json = json_encode($param);
                    $redis->set('xy:industry:data:'.$site_id.':'.$param['device_id'],$json);
                    if(!$obj)
                    {
                        return $this->json_sys_error();
                    }
            }
//        }catch (\Exception $e){
//                $redis->set('exception',$e->getMessage());
//        }
//        if($site_id < 18) {
//            $obj = InverterData::create($param);
//        }else{
//            $obj = InverterDataTwo::create($param);
//        }


        return $this->json_success();
    }

    public function postCreateConfluxBoxData(Request $request)
    {
        $param = $request->all();
        $site_id = $request->input('site_id');
        $site = Site::find($site_id);
        $param['user_id'] = $site->user_id;
//        $param['user_id'] = 3;
        $obj=ConfluxBoxData::create($param);
        if(!$obj)
        {
            return $this->json_sys_error();
        }
        return $this->json_success();
    }

    public function postCreateAmbientData(Request $request){
        $param = $request->all();
        if($request->has('site_id')) {
            $site_id = $request->input('site_id');
        }else{
            return $this->json_sys_error();
        }
        $site = Site::find($site_id);
        if($site) {
            $param['user_id'] = $site->user_id;
        }else{
            $param['user_id'] = 10000;
        }
        $obj=AmbientData::create($param);
        if(!$obj)
        {
            return $this->json_sys_error();
        }
        return $this->json_success($param);
    }
    public function postCreatePowerData(Request $request){
        $param = $request->all();
        if($request->has('site_id')) {
            $site_id = $request->input('site_id');
        }else{
            return $this->json_sys_error();
        }
        $site = Site::find($site_id);
        if($site) {
            $param['user_id'] = $site->user_id;
        }else{
            $param['user_id'] = 10000;
        }
//        $param['user_id'] = $site->user_id;
        $obj=PowerData::create($param);
        if(!$obj)
        {
            return $this->json_sys_error();
        }
        return $this->json_success($param);
    }

    private function getRedisOffsetData($site_id,$device_id,$param){
        $redis = $this->_Redis();
        $data = $redis->get("xy:industryfv:offset:".$site_id.":".$device_id);
        $re = json_decode($data,true);
        if($re) {
            $param['TotGenCap'] += $re['TotGenCap'];
            $str_time = strtotime($param['EventTime']);
            $re_str_time = strtotime($re['EventTime']);
            if (date('Y',$str_time)== date('Y',$re_str_time)) {
                $param['YearGenCap'] += $re['YearGenCap'];
            }
            if (date('m',$str_time)== date('m',$re_str_time)) {
                $param['MonthGenCap'] += $re['MonthGenCap'];
            }
            if (date('d',$str_time)== date('d',$re_str_time)) {
                $param['DayGenCap'] += $re['DayGenCap'];
            }
            return $param;
        }else{
            return $param;
        }

    }

}